/*
 * kfulles - Recognize Leaves with Android phone camera
 * Copyright (C) 2010 Oriol Piera, <oriolpiera@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id$
 */

package com.cortsenc.kfulles;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Opcions extends Activity 
		implements RadioGroup.OnCheckedChangeListener, View.OnClickListener{
	Preferencies myprefs = null;
	protected static int choice = -1;
	protected static RadioGroup rbGroup;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.set);
	    myprefs = new Preferencies(this.getApplicationContext());
	    PopulateScreen();

	    rbGroup = (RadioGroup)findViewById(R.id.RadioGroup01);
	    rbGroup.setOnCheckedChangeListener(this);
	    
	    final RadioButton rbQuery = (RadioButton)findViewById(R.id.rbQuery);
	    final RadioButton rbUpload = (RadioButton)findViewById(R.id.rbUpload);
	    final EditText usuari = (EditText)findViewById(R.id.etUser);
	    rbUpload.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Pantalla info registre
				if(usuari.length()<3){
					showDialog(1);
					rbQuery.setChecked(true);
					rbUpload.setChecked(false);
				}
			}
		});
	    
	    final Button btnDesa = (Button)findViewById(R.id.btnDesa);
	    btnDesa.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					final EditText usuari = 
									(EditText)findViewById(R.id.etUser);
					final EditText pass = (EditText)findViewById(R.id.etPass);
					if(rbQuery.isChecked()){
						Opcions.this.myprefs.setUploader(false);
						Opcions.this.myprefs.save();
						finish();
					}
					else{
						if(usuari.getText().length() == 0){
							showDialog(1);
						}
						else if(pass.getText().length() == 0){
							showDialog(1);
						}
						else{
							Opcions.this.myprefs.setUser(
												usuari.getText().toString());
							Opcions.this.myprefs.setPass(
												pass.getText().toString());
							Opcions.this.myprefs.setUploader(true);
							Opcions.this.myprefs.save();
							finish();
						}
					}	
				}
				catch (Exception e){
					Log.i("KFulles", "Failed to save settings: "
														+ e.getMessage());
				}
			}
		});
	    
	    
	    final Button btnCancela = (Button)findViewById(R.id.btnCancela);
	    btnCancela.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});	
	}
	
    public void onCheckedChanged(RadioGroup arg0, int checkedId) {
    	choice = checkedId;
    }
    
    protected AlertDialog onCreateDialog(int id) {
    	AlertDialog.Builder dialog;
        switch(id) {
        case 1:{//botoInfo
            // do the work to define the pause Dialog
        	dialog = new AlertDialog.Builder(this);
        	dialog.setTitle("kfulles");
        	dialog.setMessage(getString(R.string.infouser));
        	dialog.setIcon(R.drawable.icon);
        	dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        		public void onClick(DialogInterface dialog, int id) {
        			dialog.cancel();
               }
        	});
        	//dialog.setMessage("Are you sure you want to exit?");
        }break;
        default:
            dialog = null;
        }
        return dialog.create();
    }
    
	private void PopulateScreen() {
		// TODO Auto-generated method stub
		try{
			final EditText userfield = (EditText)findViewById(R.id.etUser);
			final EditText passfield = (EditText)findViewById(R.id.etPass);
			final RadioButton rbQuery = 
									(RadioButton)findViewById(R.id.rbQuery);
		    final RadioButton rbUpload = 
		    						(RadioButton)findViewById(R.id.rbUpload);
			userfield.setText(myprefs.getUser());
			passfield.setText(myprefs.getPass());
			if(myprefs.getUploader()){
				rbQuery.setChecked(false);
				rbUpload.setChecked(true);
			}
			else{
				rbQuery.setChecked(true);
				rbUpload.setChecked(false);
			}
		}catch (Exception e){
			Log.i("KFulles", "Failed to read settings: " + e.getMessage());
		}
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
}
